package com.example.demo.zaqizabatest.sortalgorithm;

import java.util.Arrays;

/**
 * ******************************************************
 *
 * @author liugh9
 * @version 1.0
 * @classname QuickSort
 * @description
 * @date 2023/03/28 10:22
 * <p>
 * ******************************************************
 */
public class QuickSort {

    public static void sort(int nums[]) {
        quickSort(0, nums.length - 1, nums);
        System.out.println(Arrays.toString(nums));
    }

    private static void quickSort(int left, int right, int[] nums) {
        if (right > left) {
            // 将小于target放左边，大于target放右边
            int target = nums[left];
            int l = left;
            int r = right;

            while (l <= r) {
                if (nums[l] > target) {
                    int temp = nums[r];
                    nums[r] = nums[l];
                    nums[l] = temp;
                    r--;
                } else {
                    l++;
                }
            }
            int temp = nums[l];
            nums[left] = temp;
            nums[l] = target;

            quickSort(left, l - 1, nums);
            quickSort(l + 1, right, nums);
        }

    }


    public static void main(String[] args) {
        sort(new int[]{5, 1, 1, 2, 0, 0});
    }
}
